package com.hao.crm.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hao.crm.domain.Employee;
import com.hao.crm.domain.Role;
import com.hao.crm.mapper.EmployeeMapper;
import com.hao.crm.page.PageResult;
import com.hao.crm.query.QueryObject;
import com.hao.crm.service.IEmployeeService;
@Service
public class EmployeeServiceImpl implements IEmployeeService{
	@Autowired
	private EmployeeMapper dao;
	@Override
	public int delete(Long id) {
		return dao.deleteByPrimaryKey(id);
	}

	@Override
	public int insert(Employee e) {
		int effectCount = dao.insert(e);
		List<Role> roles = e.getRoles();
		if(roles!=null) {
			for (Role role : roles) {
				dao.handlerRelation(e.getId(),role.getId());
			}
		}
		return effectCount;
	}

	@Override
	public Employee get(Long id) {
		return dao.selectByPrimaryKey(id);
	}

	@Override
	public List<Employee> selectAll() {
		return dao.selectAll();
	}

	@Override
	public int update(Employee e) {
		int effectCount = dao.updateByPrimaryKey(e);
		dao.deleteRelationByEid(e.getId());
		List<Role> roles = e.getRoles();
		if(roles!=null) {
			for (Role role : roles) {
				dao.handlerRelation(e.getId(),role.getId());
			}
		}
		return effectCount;
	}

	@Override
	public PageResult selectByCondition(QueryObject qo) {
		Long count = dao.queryByConditionCount(qo);
		if(count>0) {
			List<Employee> emps = dao.queryByCondition(qo);
			return new PageResult(count, emps);
		}
		return PageResult.Empty;
	}

	@Override
	public Employee selectBylogin(String username, String password) {
		return dao.selectBylogin(username,password);
	}

	@Override
	public int updateState(Long id) {
		return dao.updateState(id);
	}
	
}
